package com.hongning.railway.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.hongning.railway.R;
import com.hongning.railway.app.Constant;
import com.hongning.railway.entity.LoginEntity;
import com.lcodecore.tkrefreshlayout.IHeaderView;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.SPUtils;

public class Apputils {
    /**
     * 实体类转Map
     *
     * @param object
     * @return
     */
    public static Map<String, Object> entityToMap(Object object) {
        Map<String, Object> map = new HashMap();
        for (Field field : object.getClass().getDeclaredFields()) {
            try {
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                Object o = field.get(object);
                if (o != null) {
                    map.put(field.getName(), o);
                }

                field.setAccessible(flag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }
    /**
     * 获取登录后存储在本地的用户信息
     * @return
     */
   public static LoginEntity getLoginInfo(){
        String userinfo = SPUtils.getInstance().getString(Constant.userinfo,"");
        if (TextUtils.isEmpty(userinfo)){
            return null;
        }else {
            LoginEntity loginEntity = GsonUtil.GsonToBean(userinfo,LoginEntity.class);
            return loginEntity;
        }

    }

    /**
     * 获取登录后存储在本地的用户信息
     * @return
     */
    public static void clearLoginInfo(){
        SPUtils.getInstance().put(Constant.userinfo,"");
    }


    public static void setDefaultHeaderView(TwinklingRefreshLayout refreshLayout){
        ProgressLayout progressLayout = new ProgressLayout(refreshLayout.getContext());
        progressLayout.setColorSchemeColors(refreshLayout.getResources().getColor(R.color.colorPrimary));
        refreshLayout.setHeaderView(progressLayout);
    }

    /**
     * 统一封装哈获取useride1地方
     * @return
     */
    public static String getUserId(){
       LoginEntity loginEntity = getLoginInfo();
       return loginEntity.getUserinfo().getStaffId();
    }

    /**
     * 统一封装哈获取useride1地方
     * @return
     */
    public static String getGroupId(){
        LoginEntity loginEntity = getLoginInfo();
        if (loginEntity.getGroups()!=null&&!loginEntity.getGroups().isEmpty()){
            return loginEntity.getGroups().get(0).getGroupid();
        }else {
            return "";
        }

    }
    /**
     * 得到测试天气预报的测试数据
     *
     * @param mcontext
     * @param fileName
     * @return
     */
    public static String getDataInfo(Context mcontext, String fileName) {
        try {
            if (mcontext==null)
            {
                return "";
            }
            InputStream is = mcontext.getAssets().open(fileName);
            if (is==null)
            {
                return "";
            }
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String text = new String(buffer, "utf-8");
            return text;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取那个题的前面空格
     */

    public static String getBlankEmpty(){
        return "                   ";
    }

    /**
     * 获取staffid 完全不知道是啥意思
     */

    public static String getStaffid(){
        LoginEntity loginEntity = getLoginInfo();
        if (loginEntity!=null){
            return loginEntity.getUserinfo().getStaffId();
        }else {
            return "";
        }
    }

    /**
     * 獲取版本信息
     * @param context
     * @return
     */
    public static String getAppVersionName(Context context) {
        String versionName=null;
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
        } catch (Exception e) {
            KLog.e( "Exception", e);
        }
        return versionName;
    }
}
